package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.anime.ppro.enums.UserRolesEnum;
import cz.anime.ppro.logic.AttendantComponent;
import cz.anime.ppro.logic.EventComponent;
import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Attendant;
import cz.anime.ppro.model.Event;
import cz.anime.ppro.model.Webuser;

/**
 * 
 * @author Zdenek Kotal
 *
 *	Beana pro zobrazení jednoho Eventu
 */

@Component
@ManagedBean
@Scope("view")
public class EventBean {
	
	@Autowired
	private EventComponent eventComponent;
	@Autowired
	private AttendantComponent attendantComponent;
	@Autowired
	private UserComponent userComponent;
	
	private int eventID;
	
	private Date eventDate;
	private String description;
	private String text;
	
	private boolean admin = false;
	
	private Event event;
	private List<Attendant> attendants;
	private List<Webuser> registeredAttendants;
	
	@PostConstruct
	public void init(){
		
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		Map<String,String[]> params = req.getParameterMap();
		
		if (params.containsKey("event")) {
			setEventID(Integer.parseInt(params.get("event")[0]));
			setEvent(eventComponent.getEvent(eventID));
			setRegisteredAttendants(event.getWebusers());
			setAttendants(attendantComponent.getAttendantsByEventId(eventID));
			
			description = event.getDescription();
			text = event.getText();
			eventDate =  event.getEventDate();
		}
		
		if (userComponent.getUserByNick(userComponent.getCurrentUsernameFromSession()).getRole().equals(UserRolesEnum.ROLE_ADMIN.name()) ||
				userComponent.getUserByNick(userComponent.getCurrentUsernameFromSession()).equals(event.getWebuser())) {
			admin = true;
		}
	}
	
	public void deleteAttendant(int attendantID){	
		
		Attendant attendant = attendantComponent.getAttendantById(attendantID);
		
		attendantComponent.deleteAttendant(attendant);
		getAttendants().remove(attendant);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/event.jsf?event="+eventID);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void saveChanges(){
		event.setEventDate(eventDate);
		event.setDescription(description);
		event.setText(text);
		eventComponent.saveEvent(event);
	}
	
	public void addRegisteredAttendant(int value){
		Webuser newUser = userComponent.getUserByNick(userComponent.getCurrentUsernameFromSession());
		Event changeEvent = eventComponent.getEvent(value);
		changeEvent.getWebusers().add(newUser);
		eventComponent.saveEvent(changeEvent);
		
	}
	
	public Event getEvent() {
		return event;
	}
	
	public void setEvent(Event event) {
		this.event = event;
	}
	
	public List<Attendant> getAttendants() {
		return attendants;
	}
	
	public void setAttendants(List<Attendant> attendants) {
		this.attendants = attendants;
	}
	
	public List<Webuser> getRegisteredAttendants() {
		return registeredAttendants;
	}
	
	public void setRegisteredAttendants(List<Webuser> registeredAttendants) {
		this.registeredAttendants = registeredAttendants;
	}

	public int getEventID() {
		return eventID;
	}

	public void setEventID(int eventID) {
		this.eventID = eventID;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public Date getEventDate() {
		return eventDate;
	}

	public void setEventDate(Date eventDate) {
		this.eventDate = eventDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
}
